package cn.ryh.redis.singleredis.service.impl;

import cn.ryh.redis.entity.User;
import cn.ryh.redis.singleredis.dao.RedisUserDao;
import cn.ryh.redis.singleredis.service.RedisUserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @ClassName UserServiceImpl
 * @Description User服务类
 * @Author RaoYH
 * @Date 2018/10/30 17:27
 */
@Service
public class RedisUserServiceImpl implements RedisUserService {

    @Resource
    private RedisUserDao userDao;

    @Override
    @Transactional(readOnly=false)
    public User findUserById(Integer id) {
        Optional<User> user = userDao.findById(id);
        return user.get();
    }

    @Cacheable(value="redisUser")  //缓存,这里没有指定key.
    @Override
    public User redisCacheFindById(Integer id) {
        System.out.println("RedisUserServiceImpl.redisCacheFindById.从数据库中读取.");
        return userDao.findById(id).get();
    }
    @CacheEvict(value="redisUser")
    @Override
    public User redisCacheDelById(Integer id) {
        System.out.println("RedisUserServiceImpl.redisCacheDelById.从缓存中删除.");
        return null;
    }
}
